package com.bawei.library_common.image.imageloader

import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.view.RoundedCorner
import android.widget.ImageView
import androidx.annotation.RequiresApi
import com.bawei.library_common.image.ImageCrop
import com.bawei.library_common.image.ImageOptions
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.RequestOptions
import java.io.File
import java.lang.ref.WeakReference

/**
 * @Author : yaotianxue
 * @Time : On 2023/5/23 15:42
 * @Description : GlideImageLoader Glide图片加载框架
 */
class GlideImageLoader:IImageLoader {
    override fun loadFileIntoImageView(
        context: Context,
        file: File,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,file,target,config)
    }

    override fun loadUrlIntoImageView(
        context: Context,
        url: String,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,url,target,config)
    }

    override fun loadResourceIntoImageView(
        context: Context,
        source: Int,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,source,target,config)
    }

    override fun loadUriIntoImageView(
        context: Context,
        uri: Uri,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,uri,target,config)
    }

    override fun loadByteArrayIntoImageView(
        context: Context,
        bytes: Array<Byte>,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,bytes,target,config)
    }

    /**
     * 统一加载图片方法 source可以是任意类型
     */
    fun loadImageView(context: Context,
                      source: Any,
                      target: ImageView,
                      config: ImageOptions,){

        //内存泄漏:该回收的资源无法被回收掉 context是activity，当页面销毁该回收activity，但是由于Glide引用当前activity、 导致activity无法回收
        //解决方案1：使用弱应用
        //解决方案2：activity传入上下文不要传this，传入application
        var weakReference = WeakReference<Context>(context)//弱引用
        if(weakReference.get() == null){
            return
        }
        var builder = Glide.with(weakReference.get()!!).load(source)
        //解析config设置对应的builder
        setBuilderOptions(builder,config)
        builder.into(target)
    }

    /**
     * 设置图片参数
     */
    private fun setBuilderOptions(builder: RequestBuilder<Drawable>, config: ImageOptions) {
        config.let {
            var options = RequestOptions()
            if(config.placeImage != 0){//有占位符
                options.placeholder(config.placeImage)
            }
            if(config.errorImage != 0){//有加载失败图频
                options.error(config.errorImage)
            }
            config.imageSize.let {//图片大小 宽度和高度
                if(config.imageSize.size != 2){
                    throw IllegalArgumentException("please set imageSize length size is 2")
                }
                options.override(config.imageSize[0],config.imageSize[1])
            }
            if(config.skipDiskCache)  {//关闭磁盘缓存
                options.diskCacheStrategy(DiskCacheStrategy.NONE)
            }

            if(config.skipMemoryCache) {//关闭内存缓存
                options.skipMemoryCache(true)
            }
            //图片裁剪方式
            when(config.imageCrop){
                ImageCrop.CENTER -> options.centerCrop()
                ImageCrop.CIRCLE -> options.circleCrop()
              //  ImageCrop.ROUNDED -> options.transform(RoundedCorner(0,20,0,0))

                else -> {}
            }
            builder.apply(options)
        }
    }



}